// $Id: log.c 100 2011-10-21 15:05:36Z cedric.shih@gmail.com $
/*
 * Copyright 2011 Cedric Shih (cedric dot shih at gmail dot com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdarg.h>

#include "log.h"

static niohttp_logger_cb logger = NULL;

static void *arg = NULL;

void niohttp_logger_set(niohttp_logger_cb cb, void *cbarg)
{
	logger = cb;
	arg = cbarg;
}

static void niohttp_vlog(enum niohttp_log_level level,
		const char *func, unsigned int line,
		const char *fmt, va_list ap)
{
	char buffer[1024];
	vsnprintf(buffer, sizeof(buffer), fmt, ap);
	(*logger)(level, func, line, buffer, arg);
}

void niohttp_log(enum niohttp_log_level level,
		const char *func, unsigned int line, const char *fmt, ...)
{
	va_list ap;

	if (!logger) {
		return;
	}

	va_start(ap, fmt);
	niohttp_vlog(level, func, line, fmt, ap);
	va_end(ap);
}
